﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;


namespace DAL
{
    public class TemperatureHighDal
    {
        DbManager db = new DbManager();

        /// <summary>
        /// 根据reader获取数据集合
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        List<TemperatureHighModel> GetDataListByDataReader(SqlDataReader reader)
        {
            List<TemperatureHighModel> thmLi = new List<TemperatureHighModel>();
            while (reader.Read())
            {
                TemperatureHighModel thm = new TemperatureHighModel();
                thm.AotuId = int.Parse(reader["AotuId"].ToString());
                thm.BodyCondition = reader["BodyCondition"].ToString();
                thm.CreateTime = DateTime.Parse(reader["CreateTime"].ToString());
                thm.CreateUserAcount = reader["CreateUserAcount"].ToString();
                thm.DealInfo = reader["DealInfo"].ToString();
                thm.DepartId = reader["DepartId"].ToString();
                thm.DepartmentName = reader["DepartmentName"].ToString();
                thm.DepartName = reader["DepartName"].ToString();
                thm.RegistDate = DateTime.Parse(reader["RegistDate"].ToString());
                thm.Temperature = float.Parse(reader["Temperature"].ToString());
                thm.TemperatureId = Guid.Parse(reader["TemperatureId"].ToString());
                thm.UpdateTime = DateTime.Parse(reader["UpdateTime"].ToString());
                thm.userId = reader["userId"].ToString();
                thm.userName = reader["userName"].ToString();
                thmLi.Add(thm);
            }
            reader.Close();
            reader.Dispose();
            return thmLi;
        }

        /// <summary>
        /// 根据dataReader获取单挑数据
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        TemperatureHighModel GetSingleDataByDataReader(SqlDataReader reader)
        {
            TemperatureHighModel thm = new TemperatureHighModel();
            while (reader.Read())
            {
                thm.AotuId = int.Parse(reader["AotuId"].ToString());
                thm.BodyCondition = reader["BodyCondition"].ToString();
                thm.CreateTime = DateTime.Parse(reader["CreateTime"].ToString());
                thm.CreateUserAcount = reader["CreateUserAcount"].ToString();
                thm.DealInfo = reader["DealInfo"].ToString();
                thm.DepartId = reader["DepartId"].ToString();
                thm.DepartmentName = reader["DepartmentName"].ToString();
                thm.DepartName = reader["DepartName"].ToString();
                thm.RegistDate = DateTime.Parse(reader["RegistDate"].ToString());
                thm.Temperature = float.Parse(reader["Temperature"].ToString());
                thm.TemperatureId = Guid.Parse(reader["TemperatureId"].ToString());
                thm.UpdateTime = DateTime.Parse(reader["UpdateTime"].ToString());
                thm.userId = reader["userId"].ToString();
                thm.userName = reader["userName"].ToString();

            }
            reader.Close();
            reader.Dispose();
            return thm;
        }


        public List<TemperatureHighModel> GetDataList()
        {
            string sql = "select * from TemperatureHigh";
            SqlDataReader reader = db.RunDataReaderResultSql(sql);
            return GetDataListByDataReader(reader);
        }

        /// <summary>
        /// 根据id获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TemperatureHighModel GetSingleDataById(string id)
        {
            string sql = "select  BodyCondition,DealInfo,DepartId,DepartmentName,(select UnitName from Unit where UnitId=DepartId) as DepartName,RegistDate,Temperature,TemperatureId,userId,userName,AotuId,CreateTime,CreateUserAcount,UpdateTime  from TemperatureHigh where cast(TemperatureId as varchar(36)) ='" + id + "'";
            SqlDataReader reader = db.RunDataReaderResultSql(sql);
            return GetSingleDataByDataReader(reader);
        }

        /// <summary>
        /// 根据单位名，用户名获取体温异常的分页数据
        /// </summary>
        /// <param name="departId"></param>
        /// <param name="projectName"></param>
        /// <param name="userName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<TemperatureHighModel> GetDataListByDepartId(string departId, string projectName, string userName, int pageSize, int pageIndex, out int total,string beginTime,string endTime)
        {
            
            string where = "";
            if (!string.IsNullOrEmpty(projectName))
            {
                where += " and DepartName like '%" + projectName + "%'";
            }
            if (!string.IsNullOrEmpty(userName))
            {
                where += " and userName like '%" + userName + "%'";
            }
            if (!string.IsNullOrEmpty(beginTime)&&!string.IsNullOrEmpty(endTime))
            {
                where += " and RegistDate  between  '"+beginTime+"' and  '"+endTime+"'";
            }
            if (!string.IsNullOrEmpty(beginTime)&&string.IsNullOrEmpty(endTime))
            {
                where += " and  DATEDIFF(DD,RegistDate,'"+beginTime+"')=0";
            }
            string sql = "";
        
            string sqlCount = "";
            sqlCount = sql + " select COUNT(1) from TemperatureHigh where DepartId='" + departId + "'";
            sqlCount += where;

            sql += " select  * from (";
            sql += " select ROW_NUMBER() over(order by RegistDate desc) num, BodyCondition,DealInfo,DepartId,DepartmentName,(select UnitName from Unit where UnitId=DepartId) as DepartName,RegistDate,Temperature,TemperatureId,userId,userName,AotuId,CreateTime,CreateUserAcount,UpdateTime from TemperatureHigh where DepartId='"+departId+"' ";
            sql += where + " ) as c where c.num between "+(pageSize*(pageIndex-1)+1)+" and  "+pageSize*pageIndex;
            total = int.Parse(db.GetFirstData(sqlCount).ToString());
            SqlDataReader reader = db.RunDataReaderResultSql(sql);
            return GetDataListByDataReader(reader);
        }

        /// <summary>
        /// 录入单挑数据
        /// </summary>
        /// <param name="thm"></param>
        /// <returns></returns>
        public bool InsertSingleData(TemperatureHighModel thm)
        {
            string sql = "insert TemperatureHigh(DepartId,DepartName,userName,userId,DepartmentName,BodyCondition,Temperature,DealInfo,RegistDate,CreateUserAcount,UpdateTime,CreateTime) ";
            sql += "values('" + thm.DepartId + "','" + thm.DepartName + "','" + thm.userName + "','" + thm.userId + "','" + thm.DepartmentName + "','" + thm.BodyCondition + "','" + thm.Temperature + "','" + thm.DealInfo + "','" + thm.RegistDate + "','" + thm.CreateUserAcount + "','" + thm.UpdateTime + "','" + thm.CreateTime + "');";
            try
            {
                db.RunNoneResultSql(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 根据departId获取隔离和异常数
        /// </summary>
        /// <param name="departId"></param>
        /// <returns></returns>
        public int[] GetTotalCountByDepartId(string departId)
        {
            int[] intArr = new int[8];
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("departId",departId);
           DataTable dt=  db.RunDataTableResultScoreProcWithParameters("proc_GetThTotal", sqlParameters);
            intArr[0] = int.Parse(dt.Rows[0][0].ToString());
            intArr[1] = int.Parse(dt.Rows[0][1].ToString());
            intArr[2] = int.Parse(dt.Rows[0][2].ToString());
            intArr[3] = int.Parse(dt.Rows[0][3].ToString());
            intArr[4] = int.Parse(dt.Rows[0][4].ToString());
            intArr[5] = int.Parse(dt.Rows[0][5].ToString());
            intArr[6] = int.Parse(dt.Rows[0][6].ToString());
            intArr[7] = int.Parse(dt.Rows[0][7].ToString());
            return intArr;
        }

        /// <summary>
        /// 修改单条数据
        /// </summary>
        /// <param name="thm"></param>
        /// <returns></returns>
        public bool UpdateSingleDataByTempId(TemperatureHighModel thm)
        {
            string addUpdate = "";
            if (!string.IsNullOrEmpty(thm.userId))
            {
                addUpdate += " userId='"+thm.userId+"' ,";
            }
            if (!string.IsNullOrEmpty(thm.userName))
            {
                addUpdate += " userName='" + thm.userName + "' ,";
            }
            string sql = "update TemperatureHigh set "+addUpdate+"  BodyCondition='" + thm.BodyCondition + "',Temperature='" + thm.Temperature + "',DealInfo='" + thm.DealInfo + "',RegistDate='" + thm.RegistDate + "',UpdateTime='" + DateTime.Now + "' where TemperatureId='" + thm.TemperatureId + "'";
            try
            {
                db.RunNoneResultSql(sql);
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }
    }
}
